package com.znf4.controller.user;

import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.znf4.entity.ApiCommonResultVo;
import com.znf4.user.car.entity.UserCar;
import com.znf4.user.car.server.UserCarServer;

/**
 * 购物车
 */
@RestController
@RequestMapping("/user/car")
public class UserCarController {
	
	private static final Logger LOG = LoggerFactory.getLogger(UserCarController.class);
	
	@Autowired
	private UserCarServer userCarServer ;
	

	/**
	 * 函数功能说明 ： 添加购物车
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/add", method = { RequestMethod.POST, RequestMethod.GET })
	public ApiCommonResultVo add(UserCar userCar) {
		
		try {
			userCarServer.addUserCar(userCar);
		    return new ApiCommonResultVo(0, "操作成功", "" ); 
		} catch (Exception e) {
			LOG.info("添加订单接口异常 ：" + e.getMessage());
			return new ApiCommonResultVo(-2, "系统异常", "");
		}
		
	}

	
	
	/**
	 * 函数功能说明 ： 修改购物车
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/update", method = { RequestMethod.POST, RequestMethod.GET })
	public ApiCommonResultVo update(UserCar userCar) {
		
		try {
			userCarServer.updateUserCar(userCar);
		    return new ApiCommonResultVo(0, "操作成功", "" ); 
		} catch (Exception e) {
			LOG.info("添加订单接口异常 ：" + e.getMessage());
			return new ApiCommonResultVo(-2, "系统异常", "");
		}
		
	}

	/**
	 * 函数功能说明 ： 删除一个购物车商品
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/delOne", method = { RequestMethod.POST, RequestMethod.GET })
	public ApiCommonResultVo delOne(UserCar userCar, Model model) {
		
		try {
			userCarServer.delOneUserCar(userCar); 
		    return new ApiCommonResultVo(0, "操作成功", "" ); 
		} catch (Exception e) {
			LOG.info("添加订单接口异常 ：" + e.getMessage());
			return new ApiCommonResultVo(-2, "系统异常", "");
		}
		
	}

	/**
	 * 函数功能说明 ： 删除多条购物车商品
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/delAll", method = { RequestMethod.POST, RequestMethod.GET })
	public ApiCommonResultVo delAll(String[]  ids   , Model model) {
		
		try {
			List<UserCar> list = new ArrayList<>() ;
			for(int i=0 ; i < ids.length ; i ++ ){
				UserCar userCar= new UserCar() ;
				userCar.setId(ids[i]);
				list.add(userCar) ;
			}
			userCarServer.delAllUserCar(list);
		    return new ApiCommonResultVo(0, "操作成功", "" ); 
		} catch (Exception e) {
			LOG.info("添加订单接口异常 ：" + e.getMessage());
			return new ApiCommonResultVo(-2, "系统异常", "");
		}
		
	}
	

	/**
	 * 函数功能说明 ： 获取多个商品从购物车
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/queryAll", method = { RequestMethod.POST, RequestMethod.GET })
	public ApiCommonResultVo queryAll(UserCar userCar, Model model) {
		
		try {
			List<UserCar> list = userCarServer.queryAll(userCar);
		    return new ApiCommonResultVo(0, "操作成功", list ); 
		} catch (Exception e) {
			LOG.info("添加订单接口异常 ：" + e.getMessage());
			return new ApiCommonResultVo(-2, "系统异常", "");
		}
		
	}
	
	

}
